package com.zhongwang.mms.module.jiaGongStorage.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.jiaGongStorage.dao.StorageStockQueryDao;
import com.zhongwang.mms.module.jiaGongStorage.model.JStorageInventory;
import com.zhongwang.mms.module.purchase.dao.PurChangeDao;
import com.zhongwang.mms.module.purchase.dao.PurlnvQueryDao;
import com.zhongwang.mms.module.purchase.model.PChangeOrderModel;
import com.zhongwang.mms.module.purchase.model.PurInvQueryModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ExportExcelUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName： AllocationManageService
 * @Description： 库存查询业务逻辑层
 * @Author： China.zh
 * @Date： 2020/4/6 13:41
 * @Version： V1.0
 **/
@Log4j2
@Service
public class StorageStockQueryService {

    @Autowired
    private PurlnvQueryDao purlnvQueryDao;
    @Autowired
    private PurChangeDao purChangeDao;
    @Autowired
    private LogService logService;
    @Value("${user.upload.path}")
    private String uploadPath;
    @Autowired
    private StorageStockQueryDao storageStockQueryDao;

    /**
     * findPurInvList
     * 加载库存列表以及综合查询
     *
     * @param param 查询条件
     * @return
     */
    public List<JStorageInventory> findPurInvList(JStorageInventory param) {
        List<JStorageInventory> list = null;
        try {
            list = storageStockQueryDao.selectByParam(param);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }


    /**
     * 获取外购件专用仓库列表
     */
    public List<TWarehouseInfo> findWarehouseInfoListByPurchase() {
        return purlnvQueryDao.findWarehouseInfoListByPurchase();
    }

    @Value("${spring.profiles.active}")
    private String active;

    /**
     * 列表 导出
     *
     * @param param 查询条件
     * @return list
     */
    public JsonResult exportExcel(JStorageInventory param) throws IOException {
        JsonResult jsonResult = new JsonResult();
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        //存储/读取Excel的文件夹的路径 mms文件夹下/用户账户/
        String path = uploadPath + user.getAccount() + "/";
        String excelFileName = "甲供件-外购件库库存查询列表.xlsx";
        if (param.getTitle() == null || param.getTitle().equals("")) {
            param.setTitle("甲供件-外购件库库存查询");
        }
        logService.saveLog(param.getTitle() + "——导出", "用户:【" + user.getName() + "】执行导出操作！");
        try {
            //1.创建临时内存表名
            String tempTableName = "##" + user.getAccount() + (UUID.randomUUID().toString().replace("-", ""));
            param.setTempTableName(tempTableName);
            Subject userSubject = ShiroUtils.getSubject();
            //2.查询出数据导入到临时表
            storageStockQueryDao.createWarQueryTempTable(param);
            //3.导出Excel
            if (ExportExcelUtils.BuildExcel(path, excelFileName, tempTableName, active, true)) {
                jsonResult.setData(excelFileName);
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
            }
        } catch (Exception e) {
            log.debug(e.getMessage());
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }

    /**
     * @FunctionName: findPInventoryChangeOrderList
     * @Description: 查看该库存的串料信息，来自于哪个物料
     * @Author: 徐一贺
     * @CreateDate: 2020/6/13 15:11
     * @Phone: 18241927380
     * @Param: []
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Throw:
     * @Version: v 1.0.0
     */
    public List<PChangeOrderModel> findPInventoryChangeOrderList(PChangeOrderModel param) {
        return purChangeDao.findPInventoryChangeOrderList(param);
    }
}
